<?php

namespace app\company\controller;

use app\company\validate\WarehouseTakeStockValidate;
use app\service\traits\Crud;
use app\company\model\WarehouseTakeStock as WarehouseTakeStockModel;
use app\company\model\CompanyUser;
use JetBrains\PhpStorm\ArrayShape;

class WarehouseTakeStock extends Base
{
    private WarehouseTakeStockModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new WarehouseTakeStockModel();
        $this->validateClass = WarehouseTakeStockValidate::class;
        $this->append        = 'warehouse_take_stock_status_text,warehouse_take_stock_type_text';
        $this->withSearch    = 'id,no,stock_status,type,start_time,end_time,create_time,auditor_time';
        $this->isGs          = true;
    }


    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $item->creator_name   = CompanyUser::where('id', $item->creator_id)->value('real_name');//创建人
            $item->auditor_name   = CompanyUser::where('id', $item->auditor_id)->value('real_name');//审核人
            $item->operator_name  = CompanyUser::where('id', $item->operator_id)->value('real_name');//盘点操作人
            $item->start_time     = setDates($item->start_time);//盘点开始时间
            $item->end_time       = setDates($item->end_time);//盘点结束时间
            $item->auditor_time   = setDates($item->auditor_time);//审核时间
            $item->warehouse_name = $item->warehouse()->value('name');//仓库名称
        });
    }

    #[ArrayShape(['company_id' => "int", 'creator_id' => "int"])] public function addFormat(): array
    {
        return [
            'company_id' => $this->company_id,
            'creator_id' => $this->user_id
        ];
    }

}